package com.item.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.item.client.CityClient;
import com.item.pojo.City;
import com.item.pojo.Emp;
import com.item.pojo.State;
import com.item.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@RestController
public class EmpController {

    @Resource
    private EmpService empService;


    @Resource
    private CityClient client;


    //    @SentinelResource(value = "/t", blockHandlerClass = UserHandler.class, blockHandler = "block", fallbackClass = UserHandler.class, fallback = "fallback")
//    @SentinelResource(value = "/t", blockHandler = "block", fallback = "fallback")
    @RequestMapping("/findCity")
    public List<City> findAll(@RequestParam(value = "name", required = false) String name) {
        if (name.equals("admin")) {
            int i = 10 / 0;
        }
        return client.findCity();
    }


    @RequestMapping("empFindPage")
    public Map<String, Object> findPage(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                        @RequestParam(value = "limit", required = false, defaultValue = "3") Integer limit,
                                        @RequestParam(value = "empName", required = false) String empName,
                                        @RequestParam(value = "phone", required = false) String phone,
                                        @RequestParam(value = "storeName", required = false) String storeName) {
        return empService.findPage(page, limit, empName, phone, storeName);
    }


    @PostMapping("/findByName")
    public boolean findByName(String name) {
        QueryWrapper<Emp> query = new QueryWrapper<>();
        return empService.getOne(query.eq("ename", name)) != null ? true : false;
    }

    @PostMapping("/findByPhone")
    public boolean findByPhone(String phone) {
        QueryWrapper<Emp> query = new QueryWrapper<>();
        return empService.getOne(query.eq("phone", phone)) != null ? true : false;
    }

    @PostMapping("/addEmp")
    public Map<String, Object> addEmp(Emp emp) {
        System.out.println("emp:" + emp);
        emp.setStatus(0);
        emp.setAccount(emp.getName());
        emp.setPassword("123456");
        emp.setCreateDatetime(new Date());
        Map<String, Object> map = new HashMap<>();
        if (empService.saveOrUpdate(emp)) {
            map.put("result", "添加成功");
        } else {
            map.put("result", "添加失败");
        }
        return map;
    }


    @PostMapping("/findPost")
    public List<State> findPost() {
        List<State> stateList = new ArrayList<>();
        stateList.add(new State(1, "店长"));
        stateList.add(new State(2, "业务员"));
        stateList.add(new State(3, "门店实习生"));
        stateList.add(new State(4, "服务代表"));
        return stateList;
    }

    @PostMapping("/findState")
    public List<State> findState() {
        List<State> stateList = new ArrayList<>();
        stateList.add(new State(0, "在职"));
        stateList.add(new State(1, "离职"));
        return stateList;
    }

    @PostMapping("/deleteEmp")
    public boolean deleteEmp(Integer id) {
        return empService.removeById(id);
    }

    @PostMapping("/updateEmpSate")
    public boolean updateEmpSate(Integer empState, Integer id) {
        return empService.updateEmpState(id, empState);
    }

    @PostMapping("/updateEmpInfo")
    public boolean updateEmpInfo(Emp emp) {
        return empService.updateEmpInfo(emp);
    }

}
